package lock

import (
	"sync"
)

// 公平锁: 按锁请求顺序进行抢占
type fairLock struct {
	mu    sync.Mutex
	queue chan struct{}
}

func NewFairLock() *fairLock {
	return &fairLock{
		queue: make(chan struct{}, 1),
	}
}

func (f *fairLock) Lock() {
	f.queue <- struct{}{}
	f.mu.Lock()
}

func (f *fairLock) Unlock() {
	<-f.queue
	f.mu.Unlock()
}
